package cn.hxsteel.jtool.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.BeanProcessor;

/**
 * 对象工具类
 */
public class ObjectUtil
{
    private static BeanProcessor beanProcessor;
    
    public static BeanProcessor getBeanProcessor()
    {
	if ( beanProcessor == null )
	{
	    beanProcessor = new BeanProcessor();
	}
	return beanProcessor;
    }
    
    /**
     * 把结果集转实体类
     * 
     * @param rs 结果集
     * @param type 实体类
     * @return
     */
    public static < T > T toBean( ResultSet rs, Class< T > type )
    {
	try
	{
	    return getBeanProcessor().toBean( rs, type );
	}
	catch ( SQLException e )
	{
	    return null;
	}
    }
    
    /**
     * 把结果集转实体类集
     * 
     * @param rs 结果集
     * @param type 实体类
     * @return
     */
    public static < T > List< T > toBeanList( ResultSet rs, Class< T > type )
    {
	try
	{
	    return getBeanProcessor().toBeanList( rs, type );
	}
	catch ( SQLException e )
	{
	    return null;
	}
    }
}